/// @file auth_events.h
/// @brief Authentication event definitions for the modular authentication system
///
/// This file defines all the event signals and event structures used in the
/// modular authentication system with UiAo, AuthAo, and DbAo components.

#ifndef AUTH_EVENTS_H
#define AUTH_EVENTS_H

#include "qpcpp.hpp"
#include <string>
#include <cstdint>

/// @brief Authentication event signals
enum AuthSignals
{
    // UI to Auth events
    LOGIN_REQUEST_SIG = QP::Q_USER_SIG,
    REGISTER_REQUEST_SIG,
    LOGOUT_REQUEST_SIG,
    SESSION_CHECK_REQUEST_SIG,

    // Auth to UI response events
    LOGIN_RESPONSE_SUCCESS_SIG,
    LOGIN_RESPONSE_FAILURE_SIG,
    REGISTER_RESPONSE_SUCCESS_SIG,
    REGISTER_RESPONSE_FAILURE_SIG,
    LOGOUT_RESPONSE_SUCCESS_SIG,
    LOGOUT_RESPONSE_FAILURE_SIG,
    SESSION_STATUS_AUTHENTICATED_SIG,
    SESSION_STATUS_UNAUTHENTICATED_SIG,

    // Auth to DB events
    DB_USER_FIND_BY_USERNAME_SIG,
    DB_USER_CREATE_SIG,
    DB_SESSION_CREATE_SIG,
    DB_SESSION_REMOVE_SIG,
    DB_SESSION_FIND_BY_USER_ID_SIG,

    // DB to Auth response events
    DB_USER_FIND_RESPONSE_SUCCESS_SIG,
    DB_USER_FIND_RESPONSE_FAILURE_SIG,
    DB_USER_CREATE_RESPONSE_SUCCESS_SIG,
    DB_USER_CREATE_RESPONSE_FAILURE_SIG,
    DB_SESSION_CREATE_RESPONSE_SUCCESS_SIG,
    DB_SESSION_CREATE_RESPONSE_FAILURE_SIG,
    DB_SESSION_REMOVE_RESPONSE_SUCCESS_SIG,
    DB_SESSION_REMOVE_RESPONSE_FAILURE_SIG,
    DB_SESSION_FIND_RESPONSE_SUCCESS_SIG,
    DB_SESSION_FIND_RESPONSE_FAILURE_SIG
};

/// @brief Login request event
struct LoginRequestEvt : public QP::QEvt
{
    std::string username;  ///< Username for login
    std::string password;  ///< Password for login
    
    void init(const std::string& user, const std::string& pass) {
        username = user;
        password = pass;
    }
};

/// @brief Register request event
struct RegisterRequestEvt : public QP::QEvt
{
    std::string username;  ///< Username for registration
    std::string password;  ///< Password for registration
    
    void init(const std::string& user, const std::string& pass) {
        username = user;
        password = pass;
    }
};

/// @brief Logout request event
struct LogoutRequestEvt : public QP::QEvt
{
    int user_id;  ///< User ID to logout
    
    void init(int id) {
        user_id = id;
    }
};

/// @brief Session check request event
struct SessionCheckRequestEvt : public QP::QEvt
{
    std::string session_id;  ///< Session ID to check
    
    void init(const std::string& session) {
        session_id = session;
    }
};

/// @brief Database user find by username request event
struct DbUserFindByUsernameRequestEvt : public QP::QEvt
{
    std::string username;  ///< Username to find
    
    void init(const std::string& user) {
        username = user;
    }
};

/// @brief Database user create request event
struct DbUserCreateRequestEvt : public QP::QEvt
{
    std::string username;    ///< Username for new user
    std::string password;    ///< Password for new user
    std::string created_at;  ///< Creation timestamp
    std::string updated_at;  ///< Update timestamp
    
    void init(const std::string& user, const std::string& pass, 
              const std::string& created, const std::string& updated) {
        username = user;
        password = pass;
        created_at = created;
        updated_at = updated;
    }
};

/// @brief Database session create request event
struct DbSessionCreateRequestEvt : public QP::QEvt
{
    int user_id;             ///< User ID for session
    std::string token;       ///< Session token
    std::string created_at;  ///< Creation timestamp
    std::string expires_at;  ///< Expiration timestamp
    
    void init(int id, const std::string& tok, 
              const std::string& created, const std::string& expires) {
        user_id = id;
        token = tok;
        created_at = created;
        expires_at = expires;
    }
};

/// @brief Database session remove request event
struct DbSessionRemoveRequestEvt : public QP::QEvt
{
    int user_id;  ///< User ID for session removal
    
    void init(int id) {
        user_id = id;
    }
};

/// @brief Database session find by user ID request event
struct DbSessionFindByUserIdRequestEvt : public QP::QEvt
{
    int user_id;  ///< User ID to find session for
    
    void init(int id) {
        user_id = id;
    }
};

/// @brief Database user find response success event
struct DbUserFindResponseSuccessEvt : public QP::QEvt
{
    int user_id;             ///< Found user ID
    std::string username;    ///< Found username
    std::string password;    ///< Found password (hashed)
    std::string created_at;  ///< Creation timestamp
    std::string updated_at;  ///< Update timestamp
    
    void init(int id, const std::string& user, const std::string& pass, 
              const std::string& created, const std::string& updated) {
        user_id = id;
        username = user;
        password = pass;
        created_at = created;
        updated_at = updated;
    }
};

/// @brief Database user create response success event
struct DbUserCreateResponseSuccessEvt : public QP::QEvt
{
    int user_id;             ///< Created user ID
    std::string username;    ///< Created username
    std::string password;    ///< Created password (hashed)
    std::string created_at;  ///< Creation timestamp
    std::string updated_at;  ///< Update timestamp
    
    void init(int id, const std::string& user, const std::string& pass, 
              const std::string& created, const std::string& updated) {
        user_id = id;
        username = user;
        password = pass;
        created_at = created;
        updated_at = updated;
    }
};

/// @brief Database session create response success event
struct DbSessionCreateResponseSuccessEvt : public QP::QEvt
{
    int session_id;          ///< Created session ID
    int user_id;             ///< User ID for session
    std::string token;       ///< Session token
    std::string created_at;  ///< Creation timestamp
    std::string expires_at;  ///< Expiration timestamp
    
    void init(int sid, int uid, const std::string& tok, 
              const std::string& created, const std::string& expires) {
        session_id = sid;
        user_id = uid;
        token = tok;
        created_at = created;
        expires_at = expires;
    }
};

/// @brief Database session find response success event
struct DbSessionFindResponseSuccessEvt : public QP::QEvt
{
    int session_id;          ///< Found session ID
    int user_id;             ///< User ID for session
    std::string token;       ///< Session token
    std::string created_at;  ///< Creation timestamp
    std::string expires_at;  ///< Expiration timestamp
    
    void init(int sid, int uid, const std::string& tok, 
              const std::string& created, const std::string& expires) {
        session_id = sid;
        user_id = uid;
        token = tok;
        created_at = created;
        expires_at = expires;
    }
};

#endif  // AUTH_EVENTS_H